package cn.catfish.controller;

import cn.catfish.entity.User;
import cn.catfish.mapper.UserMapper;
import cn.catfish.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // 标识该类为 RESTful 控制器，返回的数据会自动转换为 JSON 格式
@RequestMapping("/user")// 定义该控制器的根路径为 /user
@Api(tags = "用户管理")
public class UserController {
    @Autowired// 自动注入 UserService，用于处理业务逻辑
    private UserService userService;

    @GetMapping("/getAlluser")// 处理 GET 请求，路径为 /user
    @ApiOperation(value = "查询所有用户")
    public List<User> getAllUser(){
        return userService.getAllUser();// 调用 UserService 获取所有用户
    }
    @GetMapping("/userById")// 处理 GET 请求
    @ApiOperation(value = "根据id查询用户")
    public User getUserById(int userid){
        return userService.getUserById(userid);
        }
    @PostMapping
    @ApiOperation(value = "添加用户")
    public void insertUser(int userid,String name,Integer age){
        User user = new User(userid, name, age,100);
        userService.insertUser(user);
    }
    @PutMapping("/update")
    @ApiOperation(value = "更新用户")
    public void updateUser(int userid,String name,Integer age){
        if (userid == 0){
            throw new RuntimeException("id不能为0");
        }
       User user = new User(userid, name, age,100);
        userService.updateUser(user);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户")
    public void deleteUser( int userid){
        userService.deleteUser(userid);
    }


}

